{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "from keras.models import Model\n",
    "from keras.layers import Input\n",
    "from keras.layers.convolutional import Conv2D\n",
    "from keras.layers.normalization import BatchNormalization\n",
    "from keras import backend as K\n",
    "from collections import OrderedDict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=6):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "DATA = OrderedDict()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pipeline 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "random_seed = 1004\n",
    "data_in_shape = (9, 9, 2)\n",
    "\n",
    "layers = [\n",
    "    Conv2D(5, (3,3), activation='relu', padding='same', strides=(2,2), data_format='channels_last', use_bias=True),\n",
    "    BatchNormalization(epsilon=1e-03, axis=-1, center=True, scale=True),\n",
    "    Conv2D(4, (1,1), activation='linear', padding='valid', strides=(1,1), data_format='channels_last', use_bias=True),\n",
    "    BatchNormalization(epsilon=1e-03, axis=-1, center=True, scale=True),\n",
    "    Conv2D(3, (3,3), activation='relu', padding='same', strides=(1,1), data_format='channels_last', use_bias=True),\n",
    "    BatchNormalization(epsilon=1e-03, axis=-1, center=True, scale=True),\n",
    "    Conv2D(2, (3,3), activation='relu', padding='valid', strides=(1,1), data_format='channels_last', use_bias=True),\n",
    "    BatchNormalization(epsilon=1e-03, axis=-1, center=True, scale=True)\n",
    "]\n",
    "\n",
    "input_layer = Input(shape=data_in_shape)\n",
    "x = layers[0](input_layer)\n",
    "for layer in layers[1:-1]:\n",
    "    x = layer(x)\n",
    "output_layer = layers[-1](x)\n",
    "model = Model(inputs=input_layer, outputs=output_layer)\n",
    "\n",
    "np.random.seed(random_seed)\n",
    "data_in = 2 * np.random.random(data_in_shape) - 1\n",
    "\n",
    "# set weights to random (use seed for reproducibility)\n",
    "weights = []\n",
    "for i, w in enumerate(model.get_weights()):\n",
    "    np.random.seed(random_seed + i)\n",
    "    if i % 6 == 5:\n",
    "        # std should be positive\n",
    "        weights.append(np.random.random(w.shape))\n",
    "    else:\n",
    "        weights.append(2 * np.random.random(w.shape) - 1)\n",
    "model.set_weights(weights)\n",
    "\n",
    "result = model.predict(np.array([data_in]))\n",
    "data_out_shape = result[0].shape\n",
    "data_in_formatted = format_decimal(data_in.ravel().tolist())\n",
    "data_out_formatted = format_decimal(result[0].ravel().tolist())\n",
    "\n",
    "DATA['pipeline_04'] = {\n",
    "    'input': {'data': data_in_formatted, 'shape': data_in_shape},\n",
    "    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],\n",
    "    'expected': {'data': data_out_formatted, 'shape': data_out_shape}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### export for Keras.js tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "filename = '../../test/data/pipeline/04.json'\n",
    "if not os.path.exists(os.path.dirname(filename)):\n",
    "    os.makedirs(os.path.dirname(filename))\n",
    "with open(filename, 'w') as f:\n",
    "    json.dump(DATA, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"pipeline_04\": {\"input\": {\"data\": [-0.922097, 0.712992, 0.493001, 0.727856, 0.119969, -0.839034, -0.536727, -0.515472, 0.231, 0.214218, -0.791636, -0.148304, 0.309846, 0.742779, -0.123022, 0.427583, -0.882276, 0.818571, 0.043634, 0.454859, -0.007311, -0.744895, -0.368229, 0.324805, -0.388758, -0.556215, -0.542859, 0.685655, 0.350785, -0.312753, 0.591401, 0.95999, 0.136369, -0.58844, -0.506667, -0.208736, 0.548969, 0.653173, 0.128943, 0.180094, -0.16098, 0.208798, 0.666245, 0.347307, -0.384733, -0.88354, -0.328468, -0.515324, 0.479247, -0.360647, 0.09069, -0.221424, 0.091284, 0.202631, 0.208087, 0.582248, -0.164064, -0.925036, -0.678806, -0.212846, 0.960861, 0.536089, -0.038634, -0.473456, -0.409408, 0.620315, -0.873085, -0.695405, -0.024465, 0.762843, -0.928228, 0.557106, -0.65499, -0.918356, 0.815491, 0.996431, 0.115769, -0.751652, 0.075229, 0.969983, -0.80409, -0.080661, -0.644088, 0.160702, -0.486518, -0.09818, -0.191651, -0.961566, -0.238209, 0.260427, 0.085307, -0.664437, 0.458517, -0.824692, 0.312768, -0.253698, 0.761718, 0.551215, 0.566009, -0.85706, 0.687904, -0.283819, 0.5816, 0.820087, -0.028474, 0.588153, -0.221145, 0.049173, 0.529328, -0.359074, -0.463161, 0.493967, -0.852793, -0.552675, -0.695748, -0.178157, 0.477995, 0.858725, 0.120384, -0.515209, 0.204484, -0.025025, -0.654961, 0.239585, -0.654691, -0.651696, -0.699951, -0.054626, -0.232999, 0.464974, 0.285499, -0.311165, 0.18009, -0.100505, 0.303943, 0.265535, -0.960747, -0.542418, 0.195178, -0.848394, 0.0774, 0.250615, -0.690541, -0.106589, -0.587335, 0.52418, -0.750735, 0.906333, -0.185252, 0.091099, -0.516456, -0.314899, -0.398607, 0.555608, 0.741523, -0.454881, 0.5701, 0.205032, -0.772784, 0.733803, -0.669988, -0.872516], \"shape\": [9, 9, 2]}, \"weights\": [{\"data\": [-0.922097, 0.712992, 0.493001, 0.727856, 0.119969, -0.839034, -0.536727, -0.515472, 0.231, 0.214218, -0.791636, -0.148304, 0.309846, 0.742779, -0.123022, 0.427583, -0.882276, 0.818571, 0.043634, 0.454859, -0.007311, -0.744895, -0.368229, 0.324805, -0.388758, -0.556215, -0.542859, 0.685655, 0.350785, -0.312753, 0.591401, 0.95999, 0.136369, -0.58844, -0.506667, -0.208736, 0.548969, 0.653173, 0.128943, 0.180094, -0.16098, 0.208798, 0.666245, 0.347307, -0.384733, -0.88354, -0.328468, -0.515324, 0.479247, -0.360647, 0.09069, -0.221424, 0.091284, 0.202631, 0.208087, 0.582248, -0.164064, -0.925036, -0.678806, -0.212846, 0.960861, 0.536089, -0.038634, -0.473456, -0.409408, 0.620315, -0.873085, -0.695405, -0.024465, 0.762843, -0.928228, 0.557106, -0.65499, -0.918356, 0.815491, 0.996431, 0.115769, -0.751652, 0.075229, 0.969983, -0.80409, -0.080661, -0.644088, 0.160702, -0.486518, -0.09818, -0.191651, -0.961566, -0.238209, 0.260427], \"shape\": [3, 3, 2, 5]}, {\"data\": [0.318429, -0.858397, -0.059042, 0.68597, -0.649837], \"shape\": [5]}, {\"data\": [0.486255, -0.547151, 0.285068, 0.764711, 0.481398], \"shape\": [5]}, {\"data\": [0.0965, 0.594443, -0.987782, 0.431322, 0.067427], \"shape\": [5]}, {\"data\": [0.228005, 0.859479, -0.49018, 0.232871, -0.303968], \"shape\": [5]}, {\"data\": [0.61488, 0.164575, 0.300991, 0.273449, 0.795127], \"shape\": [5]}, {\"data\": [-0.211487, -0.648815, -0.854588, -0.616238, -0.200391, -0.163753, 0.525164, 0.04282, -0.178234, 0.074889, -0.458875, -0.133347, 0.654533, -0.456294, 0.454776, -0.799519, -0.004428, 0.160632, 0.153349, -0.585922], \"shape\": [1, 1, 5, 4]}, {\"data\": [0.311362, -0.228519, 0.253024, -0.775634], \"shape\": [4]}, {\"data\": [-0.946541, 0.585593, -0.49527, 0.594532], \"shape\": [4]}, {\"data\": [0.114077, -0.889658, -0.472025, 0.718808], \"shape\": [4]}, {\"data\": [-0.536401, 0.404425, -0.338344, -0.818131], \"shape\": [4]}, {\"data\": [0.627511, 0.139377, 0.617668, 0.64835], \"shape\": [4]}, {\"data\": [0.677272, 0.414379, 0.565623, 0.358783, 0.401478, -0.335229, 0.52212, 0.822073, -0.215588, 0.496382, -0.508638, 0.597443, -0.380315, 0.375492, -0.491294, 0.342738, -0.671459, -0.345669, -0.372166, -0.957736, -0.46656, 0.423581, -0.318022, -0.031754, 0.556192, 0.398047, 0.601527, 0.534403, -0.299813, -0.25944, 0.698572, 0.547387, 0.558354, -0.993255, 0.26764, 0.312868, -0.885509, 0.19899, 0.252089, 0.711535, 0.607876, 0.709799, -0.17861, -0.532773, 0.123214, -0.712066, -0.366047, 0.062262, -0.236428, -0.783974, 0.824743, -0.404413, -0.963884, -0.160779, -0.363059, -0.981766, 0.580054, -0.175377, -0.475068, 0.316555, 0.04183, 0.633324, 0.822504, 0.850124, 0.583421, 0.858015, -0.295104, 0.354136, 0.055057, -0.430902, 0.190068, -0.076502, -0.836756, -0.68403, 0.024855, 0.217349, -0.392298, -0.872757, -0.58541, -0.440277, -0.168518, 0.712577, -0.736955, -0.593383, 0.543158, 0.622866, -0.667897, 0.120557, 0.018086, -0.216754, -0.573618, 0.625166, -0.630118, 0.338595, -0.761033, -0.399112, -0.437671, 0.763201, -0.854733, -0.211708, -0.562277, 0.28775, 0.749327, 0.77106, 0.689207, -0.145819, 0.476842, 0.742817], \"shape\": [3, 3, 4, 3]}, {\"data\": [-0.774929, 0.84091, -0.053971], \"shape\": [3]}, {\"data\": [-0.838065, 0.889805, 0.503326], \"shape\": [3]}, {\"data\": [-0.352161, -0.764655, -0.988392], \"shape\": [3]}, {\"data\": [0.517906, -0.666537, 0.378665], \"shape\": [3]}, {\"data\": [0.700279, 0.871936, 0.718567], \"shape\": [3]}, {\"data\": [-0.726393, 0.961405, -0.352651, -0.616831, -0.957985, 0.738251, -0.229442, -0.301669, -0.401448, -0.176988, 0.03531, -0.248273, 0.731235, -0.751996, -0.52024, 0.141734, 0.190872, 0.423504, 0.517459, 0.477292, -0.645496, -0.356895, -0.798014, -0.273988, -0.060309, 0.722704, 0.059648, -0.822663, -0.145044, 0.934283, -0.382613, -0.34684, -0.74607, -0.41484, 0.286901, 0.345101, 0.270742, 0.974401, 0.372597, 0.258112, 0.364092, -0.666525, -0.683073, 0.372326, 0.836413, -0.22059, -0.104618, 0.158763, -0.30314, -0.782504, -0.857413, 0.02191, -0.565599, 0.680123], \"shape\": [3, 3, 3, 2]}, {\"data\": [0.82814, 0.260142], \"shape\": [2]}, {\"data\": [0.295382, 0.993827], \"shape\": [2]}, {\"data\": [0.204497, 0.230931], \"shape\": [2]}, {\"data\": [-0.296706, 0.681466], \"shape\": [2]}, {\"data\": [0.109503, 0.602486], \"shape\": [2]}], \"expected\": {\"data\": [0.468145, -0.640879, 0.468145, -0.640879, 0.468145, -0.640879, 0.468145, -0.640879, 0.468145, -0.640879, 0.468145, -0.640879, 0.468145, -0.640879, 0.468145, -0.640879, 0.468145, -0.640879], \"shape\": [3, 3, 2]}}}\n"
     ]
    }
   ],
   "source": [
    "print(json.dumps(DATA))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
